<template>
  <div>
    <a-form-model ref="ruleForm" layout="vertical" :model="form" :rules="rules">
      <a-form-model-item label="店铺名称" ref="name" prop="name">
        <a-input size="large" v-model="form.name" class="input" placeholder="请输入"></a-input>
      </a-form-model-item>
      <a-form-model-item label="店铺地址" ref="address" prop="address">
        <a-input size="large" v-model="form.address" class="input" placeholder="请输入"></a-input>
      </a-form-model-item>
      <a-form-model-item label="配送时间" ref="time" prop="time">
        <a-input size="large" v-model="form.time" class="input" placeholder="请输入"></a-input>
      </a-form-model-item>
      <a-form-model-item label="联系号码" ref="phone" prop="phone">
        <a-input size="large" v-model="form.phone" class="input" placeholder="请输入"></a-input>
      </a-form-model-item>
    </a-form-model>
    <a-button type="primary" icon="check-circle" @click="save()" class="issue">
      保存
    </a-button>
  </div>
</template>

<script>
import api from '@/util/api';
import { message } from 'ant-design-vue';

export default {
  data() {
    return {
      form: {
        name: null,
        address: null,
        time: null,
        phone: null
      },
      rules: {
        name: [{ required: true, message: "请输入店铺名称", trigger: "blur" }],
        address: [{ required: true, message: "请输入店铺地址", trigger: "blur" }],
        time: [{ required: true, message: "请输入配送时间", trigger: "blur" }],
        phone: [
          { required: true, message: "请输入联系号码", trigger: "blur" },
          { pattern: /^1[3-9]\d{9}$/, message: "请输入正确的手机号码", trigger: "blur" }
        ],
      },
    }
  },
  mounted() {
    this.getStoreInfo()
  },
  methods: {
    getStoreInfo() {
      api.get("/api/shop/getInfo").then(res => {
        this.form = res.data;
      })
    },
    save() {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          let formData = new FormData();
          for (const key in this.form) {
            if (Object.prototype.hasOwnProperty.call(this.form, key)) {
              formData.append(key, this.form[key])
            }
          }
          api.post("/api/shop/updateInfo", formData).then(res => {
            message.success("保存成功");
            this.getStoreInfo()
          })
        } else {
          return false;
        }
      });
    }
  }
}
</script>